import Description from '@/components/Description';
import { realTimeMonitoringControllerDeviceBasicInfo as getInfo } from '@/services/ess/realTimeMonitoring';
import { useIntl } from '@umijs/max';
import React, { useEffect, useState } from 'react';
import { useSelectedDeviceNode } from '../SelectedDeviceNodeContext';

export const InverterBasicInfo: React.FC = () => {
  const node = useSelectedDeviceNode();
  const intl = useIntl();
  const [items, setItems] = useState<
    Array<{
      key: keyof API.DeviceManagerDTO;
      label: string;
      children: React.ReactNode;
      span?: number;
    }>
  >([
    {
      key: 'deviceName',
      label: intl.formatMessage({ id: 'baseInfo.deviceName' }), // 设备名称
      children: '',
    },
    {
      key: 'deviceModel',
      label: intl.formatMessage({ id: 'baseInfo.deviceModel' }), // 设备型号
      children: '',
    },
    {
      key: 'manufacturer',
      label: intl.formatMessage({ id: 'baseInfo.manufacturer' }), // 厂家
      children: '',
    },
    {
      key: 'maxInVol',
      label: intl.formatMessage({ id: 'baseInfo.maxInVol' }), // 最大输入电压
      children: '',
    },
    {
      key: 'inShortCircuitCurr',
      label: intl.formatMessage({ id: 'baseInfo.inShortCircuitCurr' }), // 输入短路电流
      children: '',
    },
    {
      key: 'maxInCurr',
      label: intl.formatMessage({ id: 'baseInfo.maxInCurr' }), // 最大输入电流
      children: '',
    },
    {
      key: 'maxOutCurr',
      label: intl.formatMessage({ id: 'baseInfo.maxOutCurr' }), // 最大输出电流
      children: '',
    },
    {
      key: 'ratedOutVol',
      label: intl.formatMessage({ id: 'baseInfo.ratedOutVol' }), // 额定输出电压
      children: '',
    },
    {
      key: 'ratedOutPower',
      label: intl.formatMessage({ id: 'baseInfo.ratedOutPower' }), // 额定输出功率
      children: '',
    },
    {
      key: 'outFreq',
      label: intl.formatMessage({ id: 'baseInfo.outFreq' }), // 输出频率
      children: '',
    },
    {
      key: 'powerFactor',
      label: intl.formatMessage({ id: 'baseInfo.powerFactor' }), // 功率因素
      children: '',
    },
  ]);

  const removeNullParams = (params: Record<string, any>) => {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    return Object.fromEntries(Object.entries(params).filter(([_, v]) => v !== null));
  };

  useEffect(() => {
    if (node) {
      const params = removeNullParams({
        areaId: node?.areaId ? String(node.areaId) : null,
        deviceType: node.deviceType,
        deviceCode: node.deviceCode,
      });
      // 调用后端接口获取数据
      // @ts-ignore
      getInfo(params).then((response) => {
        if (response.success && response.data) {
          const data: API.DeviceManagerDTO = response.data;

          // 更新items状态
          setItems((prevItems) =>
            prevItems.map((item) => {
              let children = data[item.key] !== undefined ? data[item.key] : item.children;

              // 特殊处理字段，添加单位
              switch (item.key) {
                case 'maxInVol':
                case 'ratedOutVol':
                  children = `${children} V`;
                  break;
                case 'inShortCircuitCurr':
                case 'maxInCurr':
                case 'maxOutCurr':
                  children = `${children} A`;
                  break;
                case 'ratedOutPower':
                  children = `${children} kW`;
                  break;
                case 'outFreq':
                  children = `${children} Hz`;
                  break;
                default:
                  break;
              }

              return {
                ...item,
                children: children,
              };
            }),
          );
        }
      });
    }
  }, [node]);

  return (
    <Description
      items={items}
      column={3}
      labelStyle={{ width: '150px' }}
      style={{ padding: '40px 80px 0 0' }}
    />
  );
};
